import moment from 'moment'

const  commonUtil = {
	//判断值是否在数组中
	isInArr : function(list,key,value){
		for(var i = 0 ; i<list.length ;i++){
				if( value==list[i][key] ){
						return true;
				}
		}
		return false;
	},
	getInArr : function(list,key,value){
		for(var i = 0 ; i<list.length ;i++){
				if( value==list[i][key] ){
						return list[i];
				}
		}
		return null;
	},
	getInArrIndex : function(list,key,value){
		for(var i = 0 ; i<list.length ;i++){
				if( value==list[i][key] ){
						return i;
				}
		}
		return -1;
	},
	getTimeKey:function(){
		var time = new Date().getTime();
		time += "" +Math.floor(Math.random()*1000+1);
		return time;
	},
	getUserAddress:function(data){
		var address =(data.districtName || '') +""+ (data.dormitoryBuildingName || '')+""+(data.dormitoryName || '')+(data.bedNo?"-"+data.bedNo:"");
		if(!address){
			address="未设置";
		}
		return address;
	},
	isPoneAvailable(str) {
		var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
		if (!myreg.test(str)) {
			return false;
		} else {
			return true;
		}
	},
	filteremoji(emojireg){
		var ranges = [
			'\ud83c[\udf00-\udfff]', 
			'\ud83d[\udc00-\ude4f]', 
			'\ud83d[\ude80-\udeff]'
		];
		emojireg = emojireg .replace(new RegExp(ranges.join('|'), 'g'), '');
		return emojireg;
	},
	uuid(){
		var s = [];
		var hexDigits = "0123456789abcdef";
		for (var i = 0; i < 36; i++) {
		  s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
		}
		s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
		s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
		s[8] = s[13] = s[18] = s[23] = "-";
	  
		var uuid = s.join("");
		return uuid;
	},
	getDate(str) {
		if(str.length==28 && str.indexOf("T")>-1 ){
			var dateStr = moment(str).utc(8).format('YYYY-MM-DD HH:mm:ss');
			dateStr = dateStr.replace(/-/g, "/");
			return new Date(dateStr);
		}
		return new Date(str);
	},
	convertImgDataToBlob(base64Data){
		var format = "image/jpeg";
		var base64 = base64Data;
		var code = window.atob(base64.split(",")[1]);
		var aBuffer = new window.ArrayBuffer(code.length);
		var uBuffer = new window.Uint8Array(aBuffer);
		for(var i = 0; i < code.length; i++){
		uBuffer[i] = code.charCodeAt(i) & 0xff ;
		}
		var blob=null;
		try{
		blob = new Blob([uBuffer], {type : format});
		}
		catch(e){
		return null
		}
		return blob;
  }
  
}
export default commonUtil;
